package org.code.mallspringboot.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.code.mallspringboot.mapper.MemberMapper;
import org.code.mallspringboot.pojo.OmsCartItem;
import org.code.mallspringboot.service.OmsCartItemService;
import org.code.mallspringboot.utils.JwtUtil;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class OmsCartItemController {

    @Autowired
    private OmsCartItemService omsCartItemService;

    @Autowired
    private MemberMapper memberMapper;

    /**
        购物车展示
     * @param request
     * @return
     */
    @GetMapping("/list")
    public CommonResult fetchCartList(HttpServletRequest request){
        // 从请求头中获取token
        String token = request.getHeader("authorization");
        token = token.replaceAll("Bearer", "");
        String userName = JwtUtil.getUserName(token);
        int memberId = memberMapper.member_id(userName);
        List<OmsCartItem> list = omsCartItemService.list(memberId);
        return CommonResult.success(list);
    }

    /**
        添加购物车
     * @param omsCartItem
     * @param request
     * @return
     */
    @PostMapping("/add")
    public CommonResult addCartItem(@RequestBody OmsCartItem omsCartItem,HttpServletRequest request){
        // 从请求头中获取token
        String token = request.getHeader("authorization");
        token = token.replaceAll("Bearer", "");
        String userName = JwtUtil.getUserName(token);
        int memberId = memberMapper.member_id(userName);
        omsCartItem.setMemberId(memberId);
        omsCartItem.setMemberNickname(userName);
        //判断添加的商品是否存在购物车-存在直接修改数量
        List<OmsCartItem> list = omsCartItemService.list(memberId);
        for (OmsCartItem cartItem : list) {
            if (cartItem.getProductId() == omsCartItem.getProductId()){
                if (omsCartItemService.update_quantity(cartItem.getId(), Integer.valueOf(cartItem.getQuantity()+1)) > 0)
                {System.out.println("修改数量成功!");return CommonResult.success(null);}
            }
        }
        if (omsCartItemService.add(omsCartItem) > 0){
            return CommonResult.success("添加成功!");
        }
        return CommonResult.failed("添加失败!");
    }

    /**
        修改商品数量
     * @param id
     * @param quantity
     * @return
     */
    @GetMapping("/update/quantity")
    public CommonResult updateQuantity(int id,int quantity){
        if (omsCartItemService.update_quantity(id, quantity) > 0){
            return CommonResult.success("修改成功!");
        }
        return CommonResult.failed("修改失败!");
    }

    /**
        删除
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult deletCartItem(int ids){
        if (omsCartItemService.delete(ids) > 0){
            return CommonResult.success("删除成功!");
        }
        return CommonResult.failed("删除失败!");
    }

    /**
        清空
     * @param request
     * @return
     */
    @PostMapping("/clear")
    public CommonResult clearCartList(HttpServletRequest request){
        // 从请求头中获取token
        String token = request.getHeader("authorization");
        token = token.replaceAll("Bearer", "");
        String userName = JwtUtil.getUserName(token);
        int memberId = memberMapper.member_id(userName);
        if (omsCartItemService.clear(memberId) > 0){
            return CommonResult.success("清空购物车成功!");
        }
        return CommonResult.failed("清空购物车失败!");
    }
}
